package cn.enbug.j2ee2;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

/**
 * Created by zhuang on 2017/12/5.
 */
public class LoginController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("login.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (password.equals(State.users.get(username))) {
            req.getSession().setAttribute("username", username);
            resp.sendRedirect("/");
        } else {
            resp.sendRedirect("/login.html#");
        }
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        State.WEBINF = getServletContext().getRealPath("/WEB-INF/");
        ArrayList<String> lines = new ArrayList<>();
        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(State.WEBINF + "/user.txt"));
            String s;
            while ((s = br.readLine()) != null) {
                lines.add(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (String str : lines) {
            int pos = str.indexOf("=");
            State.users.put(str.substring(0, pos), str.substring(pos + 1));
        }
        lines.clear();
        try {
            br = new BufferedReader(new FileReader(State.WEBINF + "/login.txt"));
            String s;
            while ((s = br.readLine()) != null) {
                lines.add(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (String str : lines) {
            int pos = str.indexOf("=");
            State.logins.put(str.substring(0, pos), str.substring(pos + 1));
        }
    }

    @Override
    public void destroy() {
        super.destroy();
        FileWriter fw = null;
        try {
            fw = new FileWriter(State.WEBINF + "/login.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : State.logins.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue()).append(System.lineSeparator());
        }
        String s = sb.toString();
        try {
            assert fw != null;
            fw.write(s, 0, s.length());
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
